<?php

# http://projects:8081/cdapp-2kt-api/api/seat_sector_record.php

include_once '_portal.php';

forbidden_acc();

$startTime = isset($post->startTime) ? $post->startTime : date('Y-m-d H:i:s', time() - 24 * 3600);
$endTime   = isset($post->endTime) ? $post->endTime : date('Y-m-d H:i:s');
$action    = isset($post->action) ? $post->action : 'sector';

output(main($startTime, $endTime, $action));

function main($startTime, $endTime, $action) {
  switch ($action) {
  case 'sector':return sector($startTime, $endTime);
  case 'runway':return sector($startTime, $endTime);
  default: return ['msg' => 'Error, invalid module provided!'];
  }
}

function sector($startTime, $endTime) {
  $sql = "SELECT * FROM 2kt_sector_record WHERE `startTime` <= '{$endTime}' AND (`endTime` >= '{$startTime}' OR `endTime` IS NULL)";

  $result = exec_sql($sql);

  $success = ($result && is_array($result)) ? true : false;

  $count = $success ? count($result) : 0;

  $content = $success ? query_parse($result) : [];

  return ['success' => $success, 'count' => $count, 'content' => $content, 'sql' => $success ? '' : $sql];
}

function runway($startTime, $endTime) {
  $sql = "SELECT * FROM 2kt_runway_record WHERE `startTime` <= '{$endTime}' AND (`endTime` >= '{$startTime}' OR `endTime` IS NULL)";

  $result = exec_sql($sql);

  $success = ($result && is_array($result)) ? true : false;

  $count = $success ? count($result) : 0;

  $content = $success ? query_parse($result) : [];

  return ['success' => $success, 'count' => $count, 'content' => $content, 'sql' => $success ? '' : $sql];
}